package com.inossem.warehousemanage_tv.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.bean.PositionsBean;

import java.util.List;

/**
 * 仓库概览-仓位-列表
 *
 * @author 詹建宇
 * @time on 2019/2/14 12:06
 * @email jianyu.zhan@inossem.com
 */
public class PositionsAdapter extends BaseAdapter {

    private Context context;
    private List<PositionsBean.LayerListBean.PositionListBean> list;
    private OnItemClickListener listener;

    public PositionsAdapter(Context context, List<PositionsBean.LayerListBean.PositionListBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_ware_positions_position_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        PositionsBean.LayerListBean.PositionListBean bean = list.get(position);
        viewHolder.positionTv.setText(bean.getPosition_index3());

        String matName = bean.getMat_name();

        if (TextUtils.isEmpty(matName)) {
            viewHolder.matNameTv.setBackgroundResource(R.color.mat_null);
            viewHolder.matNameTv.setText("空");
        } else {
            viewHolder.matNameTv.setBackgroundResource(R.color.base_blue);
            viewHolder.matNameTv.setText(matName);
        }

        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(matName) && null != listener)
                    listener.onItemClick(position, bean);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private LinearLayout layout;
        private TextView matNameTv, positionTv;

        public ViewHolder(View view) {
            layout = view.findViewById(R.id.layout);
            matNameTv = view.findViewById(R.id.matNameTv);
            positionTv = view.findViewById(R.id.positionTv);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, PositionsBean.LayerListBean.PositionListBean positionBean);
    }

}
